package com.example.project7;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import com.example.project7.fragment.HomeFragment;
import com.example.project7.fragment.MineFragment;
import com.example.project7.model.ListItem;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity implements MineFragment.OnInterestSelectedListener, HomeFragment.OnListItemClickListener {
    private static final String TAG = "MainActivity";
    
    private BottomNavigationView bottomNavigation;
    private HomeFragment homeFragment;
    private MineFragment mineFragment;
    private String username;
    private String interest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");
        setContentView(R.layout.activity_main_fragment);

        // 获取传递的用户名
        username = getIntent().getStringExtra("username");
        
        // 从SharedPreferences获取兴趣信息
        SharedPreferences sharedPreferences = getSharedPreferences("user_info", MODE_PRIVATE);
        interest = sharedPreferences.getString("interest", "未选择");

        // 初始化底部导航
        bottomNavigation = findViewById(R.id.bottom_navigation);
        bottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                int itemId = item.getItemId();
                if (itemId == R.id.nav_home) {
                    loadFragment(homeFragment);
                    return true;
                } else if (itemId == R.id.nav_mine) {
                    loadFragment(mineFragment);
                    return true;
                }
                return false;
            }
        });

        // 初始化Fragment
        initFragments();
        
        // 默认选中首页
        bottomNavigation.setSelectedItemId(R.id.nav_home);
    }

    /**
     * 初始化Fragment
     */
    private void initFragments() {
        // 从SupportFragmentManager中查找已存在的Fragment
        homeFragment = (HomeFragment) getSupportFragmentManager().findFragmentByTag("HOME");
        mineFragment = (MineFragment) getSupportFragmentManager().findFragmentByTag("MINE");
        
        // 如果不存在则创建新的Fragment
        if (homeFragment == null) {
            homeFragment = new HomeFragment();
        }
        if (mineFragment == null) {
            mineFragment = new MineFragment();
        }
        
        // 更新Fragment数据
        updateFragmentData();
    }

    /**
     * 加载Fragment
     */
    private void loadFragment(Fragment fragment) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        
        // 隐藏所有Fragment
        if (homeFragment.isAdded()) {
            transaction.hide(homeFragment);
        }
        if (mineFragment.isAdded()) {
            transaction.hide(mineFragment);
        }
        
        // 显示目标Fragment
        if (fragment.isAdded()) {
            transaction.show(fragment);
        } else {
            transaction.add(R.id.fragment_container, fragment, 
                fragment instanceof HomeFragment ? "HOME" : "MINE");
        }
        
        transaction.commit();
    }

    /**
     * 更新Fragment数据
     */
    private void updateFragmentData() {
        if (homeFragment != null) {
            homeFragment.updateInterest(interest);
        }
        if (mineFragment != null) {
            mineFragment.updateUserInfo(username, interest);
        }
    }

    @Override
    public void onInterestSelected(String interest) {
        this.interest = interest;
        
        // 保存兴趣到SharedPreferences
        SharedPreferences sharedPreferences = getSharedPreferences("user_info", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("interest", interest);
        editor.apply();
        
        // 更新首页Fragment显示
        if (homeFragment != null) {
            homeFragment.updateInterest(interest);
        }
    }
    
    @Override
    public void onListItemClick(ListItem item) {
        // 跳转到详情页
        Intent intent = new Intent(this, DetailActivity.class);
        intent.putExtra("list_item", item);
        startActivity(intent);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG, "onRestart");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
    }
}